﻿/**************************************************************

* Copyright (C) 2010 iTouch, Inc (http://www.cnblogs.com/Leo_wl)

* All Rights Reserved

* Created by Leo.wl <QQ:114818988>

 

* 版权所有：Microsoft Corporation.

* 作    者：Leo.wl(nmgimust_wl@163.com)

* 创建时间：2010-06-11

 

* 修 改 人：

* 修改内容：

* 修改时间：

**************************************************************/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.ApplicationBlocks.Data;
using GotDotNet.ApplicationBlocks.Data;
using System.Data.SqlClient;

using LibraryIDAL;
using LibraryUtility.Web;
using LibraryModels;
using LibraryUtility;
using System.Data;
using Maticsoft.DBUtility;
using LibraryUtility.Data;

namespace LibraryDAL
{
    /// <summary>
    /// ClassName:TabBookType
    /// Auther:Leo.wl
    /// Time:2010-04-16
    /// 数据访问类TabBookType。
    /// </summary>
    public  class TabBookType:ITabBookType
    {
        /// <summary>
        /// string readonly static connString.
        /// </summary>
        private static readonly string connString = ConfigHelper.GetConfigString("SqlServerConnectionString");
        /// <summary>
        /// Sql Helper
        /// </summary>
        private static readonly LibraryUtility.Common.SqlDBHelper sqlDbHelper = new LibraryUtility.Common.SqlDBHelper();
        /// <summary>
        /// Sql parameter
        /// </summary>
        private SqlParameter para;


        public TabBookType()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int TypeID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@TypeID", SqlDbType.Int,4)};
            parameters[0].Value = TypeID;

            int result = DbHelperSQL.RunProcedure("UP_TabBookType_Exists", parameters, out rowsAffected);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public int Add(LibraryModels.TabBookType model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@TypeID", SqlDbType.Int,4),
					new SqlParameter("@TypeName", SqlDbType.NVarChar,50),
					new SqlParameter("@Remark", SqlDbType.NVarChar,250)};
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.TypeName;
            parameters[2].Value = model.Remark;

            DbHelperSQL.RunProcedure("UP_TabBookType_ADD", parameters, out rowsAffected);
            return (int)parameters[0].Value;
        }

        /// <summary>
        ///  更新一条数据
        /// </summary>
        public int Update(LibraryModels.TabBookType model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@TypeID", SqlDbType.Int,4),
					new SqlParameter("@TypeName", SqlDbType.NVarChar,50),
					new SqlParameter("@Remark", SqlDbType.NVarChar,250)};
            parameters[0].Value = model.TypeID;
            parameters[1].Value = model.TypeName;
            parameters[2].Value = model.Remark;

            DbHelperSQL.RunProcedure("UP_TabBookType_Update", parameters, out rowsAffected);
            return rowsAffected;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int Delete(int TypeID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@TypeID", SqlDbType.Int,4)};
            parameters[0].Value = TypeID;

            DbHelperSQL.RunProcedure("UP_TabBookType_Delete", parameters, out rowsAffected);
            return rowsAffected;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public LibraryModels.TabBookType GetModel(int TypeID)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@TypeID", SqlDbType.Int,4)};
            parameters[0].Value = TypeID;

            LibraryModels.TabBookType model = new LibraryModels.TabBookType();
            DataSet ds = DbHelperSQL.RunProcedure("UP_TabBookType_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["TypeID"].ToString() != "")
                {
                    model.TypeID = int.Parse(ds.Tables[0].Rows[0]["TypeID"].ToString());
                }
                model.TypeName = ds.Tables[0].Rows[0]["TypeName"].ToString();
                model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select TypeID,TypeName,Remark ");
            strSql.Append(" FROM TabBookType ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" TypeID,TypeName,Remark ");
            strSql.Append(" FROM TabBookType ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "TabBookType";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }

        /// <summary>
        /// 页面的分页
        /// </summary>
        /// <param name="fldStr"></param>
        /// <param name="strWhere"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="keyFld"></param>
        /// <param name="strSort"></param>
        /// <param name="isGetCount"></param>
        /// <returns></returns>
        public DataSet GetList(string fldStr, string strWhere, int PageIndex, int PageSize, string keyFld, string strSort, int isGetCount)
        {
            SqlParameter[] parameters = {
											new SqlParameter("@tab", SqlDbType.VarChar, 1000),
											new SqlParameter("@strFld", SqlDbType.VarChar, 1500),
											new SqlParameter("@strWhere",SqlDbType.VarChar,5000),
											new SqlParameter("@Page", SqlDbType.Int),
											new SqlParameter("@RecsPerPage", SqlDbType.Int),
											new SqlParameter("@ID", SqlDbType.VarChar,255),
											new SqlParameter("@Sort", SqlDbType.VarChar,255),
											new SqlParameter("@isGetCount", SqlDbType.Bit),
			};

            parameters[0].Value = "TabBookType";
            parameters[1].Value = fldStr;
            parameters[2].Value = strWhere;
            parameters[3].Value = PageIndex;
            parameters[4].Value = PageSize;
            parameters[5].Value = keyFld;
            parameters[6].Value = strSort;
            parameters[7].Value = isGetCount;


            return Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteDataset(connString, "Pagination_LeoWL", parameters);

        }

        /// <summary>
        /// 页面的分页
        /// </summary>
        /// <param name="fldStr">The FLD STR.</param>
        /// <param name="strWhere">The STR where.</param>
        /// <param name="PageIndex">Index of the page.</param>
        /// <param name="PageSize">Size of the page.</param>
        /// <param name="keyFld">The key FLD.</param>
        /// <param name="strSort">The STR sort.</param>
        /// <param name="isGetCount">The is get count.</param>
        /// <param name="TableName">Name of the table.</param>
        /// <returns></returns>
        public DataSet GetList(string fldStr, string strWhere, int PageIndex, int PageSize, string keyFld, string strSort, int isGetCount, string TableName)
        {
            SqlParameter[] parameters = {
											new SqlParameter("@tab", SqlDbType.VarChar, 1000),
											new SqlParameter("@strFld", SqlDbType.VarChar, 1500),
											new SqlParameter("@strWhere",SqlDbType.VarChar,5000),
											new SqlParameter("@Page", SqlDbType.Int),
											new SqlParameter("@RecsPerPage", SqlDbType.Int),
											new SqlParameter("@ID", SqlDbType.VarChar,255),
											new SqlParameter("@Sort", SqlDbType.VarChar,255),
											new SqlParameter("@isGetCount", SqlDbType.Bit),
			};

            parameters[0].Value = TableName;
            parameters[1].Value = fldStr;
            parameters[2].Value = strWhere;
            parameters[3].Value = PageIndex;
            parameters[4].Value = PageSize;
            parameters[5].Value = keyFld;
            parameters[6].Value = strSort;
            parameters[7].Value = isGetCount;


            return Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteDataset(connString, "Pagination_LeoWL", parameters);

        }

        /// <summary>
        /// 查询所有图书类型(查询图书类型用到,带搜索)
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public DataSet GetAllBookType(string message)
        {
            DataSet ds = new DataSet();
            try
            {
                SqlParameter[] sp ={
                    para = new SqlParameter("@message",message)
                };
                ds = sqlDbHelper.GetDataSet("GetAllBooKType", sp);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ds;
        }

        /// <summary>
        /// 根据图书类型编号，获取图书类型。
        /// </summary>
        /// <param name="TypeId">图书类型编号</param>
        /// <returns></returns>
        public DataSet GetRoomByTypeId(int TypeId)
        {
            DataSet ds = new DataSet();
            try
            {
                SqlParameter[] sp ={
                    para = new SqlParameter("@TypeId",TypeId)
                };
                ds = sqlDbHelper.GetDataSet("GetRoomByTypeId", sp);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ds;
        }
        #endregion  成员方法

        
    }
}
